/* */
define(
		[ 'jquery' ],
		function($) {

			var ProductType = function(options) {
				/*this.initproid = options.initproid;
				this.initcid = options.initcid;
				this.initdid = options.initdid;
				this.wrapper = options.wrapper;
				this.province = options.provinceWrapper;
				this.city = options.cityWrapper;
				this.district = options.districtWrapper;
				this.town = options.townWrapper;
				this.groupItems = this.wrapper.find('[role=group]');
				this.isShowAllBtn = options.isShowAllBtnWrapper; 
				this.init();*/
				this.initfirsttype = options.initfirsttype;
				this.initsecondtype = options.initsecondtype;
				this.initthiredtype = options.initthiredtype;
				this.wrapper = options.wrapper;
				this.firstlevel = options.firstlevelWrapper;
				this.secondlevel = options.secondlevelWrapper;
				this.thiredlevel = options.thiredlevelWrapper; 
				this.groupItems = this.wrapper.find('[role=group]');
				this.isShowAllBtn = options.isShowAllBtnWrapper; 
				this.init();
			};

			ProductType.prototype.init = function() {
				var _pid=this.initfirsttype,
				_cid=this.initsecondtype,
				_did=this.initthiredtype,
				_tid,
				$firstlevel = this.firstlevel, 
				$secondlevel = this.secondlevel, 
				$thiredlevel = this.thiredlevel, 
//				$town = this.town,
				$isShowAllBtn = this.isShowAllBtn,
				allDataOption = "<li><a data-type='all' href=javascript:;>全部</a></li>",
				_this = this;
				$
						.getJSON(
								ctx + "/common/producttype",
								{
									level:1
								},
								function(data) {
									var jsonObj = data.producttype;
									for (var i = 0; i < jsonObj.length; i++) {
										var $option = $('<li><a href="javascript:;"></a></li>');
										var row = jsonObj[i];
										$option.find('a').data("proid", row.id)
												.attr('data-proid', row.id);
										$option.find('a').text(row.typeName);
										$firstlevel.append($option);
										if(row.id==_pid)
							        	  	text_=row.typeName;
									}
									if(_pid!=null&&_pid!=''){ 
							    	  	$firstlevel.prev().html(text_+"<span class='caret'></span>");
							      }
								});
				if (this.initsecondtype != null && this.initsecondtype != '') {
					$
							.getJSON(
									ctx + "/common/producttype",
									{
										parentId : this.initfirsttype
									},
									function(data) {
										var jsonObj = data.producttype;
										for (var i = 0; i < jsonObj.length; i++) {
											var $option = $('<li><a href="javascript:;"></a></li>');
											var row = jsonObj[i];
											$option.find('a').data("cid",
													row.id).attr('data-cid',
													row.id);
											$option.find('a').text(row.typeName);
											$secondlevel.append($option);
											if(row.id==_cid)
								        	  	text_=row.typeName;
										}
										$secondlevel.prev().html(text_+"<span class='caret'></span>");
									});
				}
				if (this.initthiredtype != null && this.initthiredtype != '') {
					$
							.getJSON(
									ctx + "/common/producttype",
									{
										parentId : this.initsecondtype
									},
									function(data) {
										var jsonObj = data.producttype;
										for (var i = 0; i < jsonObj.length; i++) {
											var $option = $('<li><a href="javascript:;"></a></li>');
											var row = jsonObj[i];
											$option.find('a').data("did",
													row.id).attr('data-did',
													row.id);
											$option.find('a').text(row.typeName);
											$thiredlevel.append($option);
											console.log(row.id+";" + _did);
											if(row.id==_did)
								        	  	text_=row.typeName;
										}
										
									});
				}
				/* 根据省份获取分类 */
				$firstlevel.on('click', 'a', function() {
					var _txt = $(this).text();
					_pid = $(this).data('proid');
					
					$firstlevel.siblings('.input-hidden').val(_pid);//input赋值
					$firstlevel.siblings('.input-hidden').data('name',_txt)
					
					$secondlevel == null ? "" : $secondlevel.html("");
					$secondlevel.prev().attr('disabled', false).children(
							'.option-name').text("选择分类");
					$thiredlevel == null ? "" : $thiredlevel.html("");
					$thiredlevel.prev().attr('disabled', false).children(
							'.option-name').text("选择分类");
//					if ($town != null) {
//						$town.html("");
//						$town.prev().attr('disabled', false).children(
//								'.option-name').text("选择街道、乡镇");
//					}
					_this.addAdvice({
						nextId : $secondlevel,
						currentText : _txt,
						paraObj : {
							parentId : _pid
						},
						replaceText : '选择分类',
						url : ctx + "/common/producttype",
						currentDataId : 'proid',
						nextDataId : 'cityid',
						dataId : _pid
					});
				});
				/* 根据分类获取区、县 */
				$secondlevel.on('click', 'a', function() {
					var _txt = $(this).text();
					_cid = $(this).data('cityid');
					$secondlevel.siblings('.input-hidden').val(_cid);//input赋值
					$secondlevel.siblings('.input-hidden').data('name',_txt)
					
					$thiredlevel == null ? "" : $thiredlevel.html("");
					$thiredlevel.prev().attr('disabled', false).children(
							'.option-name').text("选择分类");
//					if ($town != null) {
//						$town.html("");
//						$town.prev().attr('disabled', false).children(
//								'.option-name').text("选择街道、乡镇");
//					}
					
					_this.addAdvice({
						nextId : $thiredlevel,
						currentText : _txt,
						paraObj : {
							parentId : _cid
						},
						replaceText : '选择分类',
						url : ctx + "/common/producttype",
						currentDataId : 'cityid',
						nextDataId : 'did',
						dataId : _cid
					});
				});

				/* 根据分类获取乡镇 */
				$thiredlevel.on('click', 'a', function() {
					var _txt = $(this).text();
					_did = $(this).data('did');
					$thiredlevel.siblings('.input-hidden').val(_did);//input赋值
					$thiredlevel.siblings('.input-hidden').data('name',_txt);
					
//					if ($town != null) {
//						$town.html("");
//						$town.prev().attr('disabled', false).children(
//								'.option-name').text("选择街道、乡镇");
//					}
					
					if($(this).data('type')=='all') {
						$('.dropdown-menu').css('display','none');
						$thiredlevel.siblings('.input-hidden').data('name','');
//						$town.siblings('.input-hidden').data('name','');
						/** 设定pcdt input的显示值 **/
						var showpcdt_inputname = $thiredlevel.siblings('.input-hidden').data("showpcdt");
						var pcdt_inputclass = $thiredlevel.siblings('.input-hidden').data("pcdtclass");
						var senderdistrict = '';
						$("."+pcdt_inputclass).each(function(a,b){
					        var name = $(b).data("name");
					        if(name!=null && name!="") {
					        	senderdistrict += name+"-";
					        }
					    });
						$("input[name="+showpcdt_inputname+"]").val(senderdistrict.substring(0,senderdistrict.length-1));
						/** 设定pcdt input的显示值 **/
					} else {
//						_this.addAdvice({
//							nextId : $town,
//							currentText : _txt,
//							paraObj : {
//								did : _did
//							},
//							replaceText : '选择街道、乡镇',
//							url : ctx + "/common/producttype",
//							currentDataId : 'did',
//							nextDataId : 'tid',
//							dataId : _did
//						});
					}
				});
				
				/* 根据分类获取乡镇 */
//				$town.on('click', 'a', function() {
//					var _txt = $(this).text();
//					_tid = $(this).data('tid');
//					$town.siblings('.input-hidden').val(_tid);//input赋值
//					$town.siblings('.input-hidden').data('name',_txt);
//					
//						var showpcdt_inputname = $town.siblings('.input-hidden').data("showpcdt");
//						var pcdt_inputclass = $town.siblings('.input-hidden').data("pcdtclass");
//						var senderdistrict = '';
//						$("."+pcdt_inputclass).each(function(a,b){
//					        var name = $(b).data("name");
//							senderdistrict += name+"-";
//					    });
//					if($(this).data('type')=='all') {
//						$town.siblings('.input-hidden').data('name','')
//						$("input[name="+showpcdt_inputname+"]").val(senderdistrict.substring(0,senderdistrict.length-4));
//						$('.dropdown-menu').css('display','none');
//					}else{
//						$("input[name="+showpcdt_inputname+"]").val(senderdistrict.substring(0,senderdistrict.length-1));
//					}
//				});

				ProductType.prototype.addAdvice = function(option) {
					$.getJSON(option.url,
								option.paraObj,
								function(data) {
									console.log(data);
									if (data == '')
										return;
									var jsonObj = null;
									if (option.nextDataId == 'cityid') {
										jsonObj = data.producttype;
									}else if (option.nextDataId == 'did'){
										if($isShowAllBtn==1) {
											option.nextId.append(allDataOption);
											$("input[name='townid']").val('')
										}
										jsonObj = data.producttype;
									}else if (option.nextDataId == 'tid'){
										if($isShowAllBtn==1) {
											option.nextId.append(allDataOption);
											
										}
										jsonObj = data.producttype;
									}	
									for (var i = 0; i < jsonObj.length; i++) {
										var $option = $("<li><a href=javascript:;></a></li>");
										var row = jsonObj[i];
										if (option.nextDataId == 'cityid')
											$option.find('a').data("cid",
													row.id).attr(
													'data-cid', row.id);
										else if (option.nextDataId == 'did')
											$option.find('a').data("did",
													row.id).attr(
													'data-did', row.id);
										else if (option.nextDataId == 'tid')
											$option.find('a').data("tid",
													row.id).attr(
													'data-tid', row.id);
										$option.find('a').data(
												option.nextDataId, row.id);
										$option.find('a').text(row.typeName);
										option.nextId.append($option);
									}
								});
				};
			}
			return {
				ProductType : ProductType
			}
		});